package cn.cjj.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.cjj.gulimall.product.entity.AttrEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.cjj.gulimall.product.entity.CategoryEntity;
import cn.cjj.gulimall.product.service.CategoryService;
import cn.cjj.common.utils.PageUtils;
import cn.cjj.common.utils.R;



/**
 * 商品三级分类
 *
 * @author cjj
 * @email cjj@gmail.com
 * @date 2021-05-27 20:45:15
 */
@RefreshScope
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Value("${module.name}")
    private String moduleName;

    @RequestMapping("module/name")
    public R getModuleName() {
        return R.ok().put("moduleName", moduleName);
    }


    @RequestMapping("/testPost")
    public R delete(@RequestBody AttrEntity[] catIds){
        for (AttrEntity catId : catIds) {
            System.out.println(catId);
        }

        return R.ok();
    }


    @RequestMapping("/list/tree")
    public R listTree() {
        List<CategoryEntity> entities = categoryService.listWithTree();

        List<CategoryEntity> level1Menus = entities.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu -> {
                    menu.setChildren(getChildren(menu, entities));
                    return menu;
                })
                .sorted((m1, m2) -> {
                    return (m1 == null ? 0 : m1.getSort()) - (m2 == null ? 0 : m2.getSort());
                })
                .collect(Collectors.toList());


        return R.ok().put("data", level1Menus);
    }

    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == root.getCatId())
                .map(menu -> {
                    menu.setChildren(getChildren(menu, all));
                    return menu;
                })
                .sorted((m1, m2) -> {
                    return (m1 == null ? 0 : m1.getSort()) - (m2 == null ? 0 : m2.getSort());
                })
                .collect(Collectors.toList());
        return children;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
